window.addEventListener('load', function () {
    let tbody = document.querySelector('table.layui-table>tbody')
    let divData = {
        pagenum: 1,
        pagesize: 5
    }
    let total
    //渲染数据
    let init = function () {
        obj.url = '/my/article/list'
        obj.type = 'get'
        obj.data = divData
        obj.success = function (res) {
            if (res.status === 0) {
                layer.msg(res.message, { time: 1000 })
                render(res)
                initSelect()
            }
            else return layer.msg(res.message, { time: 1000 })
        }
        myAxios(obj)
    }
    function render(data) {
        template.defaults.imports.dateFormat = function (data) {
            let daa = new Date(data)
            return daa.toLocaleDateString().replace(/\//g, '-')
        }
        console.log(data.total);
        let str = template('tpl-table', data)
        tbody.innerHTML = str
        total = data.total
        divPage()
    }
    init()
    //删除事件
    tbody.addEventListener('click', function (e) {
        if (e.target.textContent === '删除') {
            //确认删除
            layer.confirm('确定删除？', { icon: 3, title: '提示' }, function (index) {
                deleteArt(e.target.dataset.id)
                layer.close(index);
            });
        }
    })
    //删除函数
    function deleteArt(id) {
        obj.type = 'get'
        obj.url = '/my/article/delete/' + id
        obj.success = function (res) {
            layer.msg(res.message, {
                time: 1000
            })
            init()
        }
        myAxios(obj)
    }
    //编辑事件
    tbody.addEventListener('click', function (e) {
        if (e.target.textContent === '编辑') {
            console.log('开始编辑', e.target.dataset.id);
            //跳转到发布页面
            window.parent.document.querySelector('iframe').src = '/article/art_pub.html?id=' + e.target.dataset.id
        }
    })
    //下拉列表框
    let select = document.querySelector('select[name="cate_id"]')
    let initSelect = function () {
        obj.url = '/my/article/cates'
        obj.type = 'get'
        obj.data = null
        obj.success = function (res) {
            if (res.status === 0) {
                renderSelect(res.data)
            }
            else return layer.msg(res.message, { time: 1000 })
        }
        myAxios(obj)
        function renderSelect(data) {
            let str1 = template('tpl-cate', { data: data })
            select.innerHTML = str1
            layui.use('form', function () {
                var form = layui.form
                form.render()
            })
            // let srt = document.createElement('script')
            // srt.src = '/assets/lib/layui/layui.all.js'
            // let body = document.body
            // body.appendChild(srt)
        }
    }
    //实现筛选功能
    layui.use('form', function () {
        var form = layui.form;
        form.on('submit(formSel)', function (data) {
            obj.url = '/my/article/list'
            obj.type = 'get'
            divData.cate_id = data.field.cate_id
            divData.state = data.field.state
            obj.data = divData
            obj.success = function (res) {
                layer.msg(res.message, { time: 1000 })
                render(res)
            }
            myAxios(obj)
            return false;
        });
    });
    //分页功能
    function divPage() {
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage.render({
                elem: 'pageBox',
                count: total,
                limit: divData.pagesize,
                curr: divData.pagenum,
                limits: [10, 20, 30],
                jump: function (obj, first) {
                    divData.pagenum = obj.curr
                    if (!first) {
                        init()
                    }
                }
            });
        });
    }
})